package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.heima.media.service.IWmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author: itheima
 * @create: 2021-12-20 11:45
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {

    @Autowired
    private IWmNewsService wmNewsService;

    /**
     * 分页根据名称模糊查询频道列表
     *
     * @param dto 包含频道名称，分页信息
     * @return
     */
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        //int i = 1 / 0;
        //1.构建封装条件QueryWrapper
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }

        //2.构建分页对象Page - 封装分页 页码，页大小
        IPage<WmChannel> ipage = new Page<>(dto.getPage(), dto.getSize());

        //3.执行调用业务层（公共）service中分页方法 执行完分页后Ipage得到总记录数，总页数，当前页数据
        ipage = this.page(ipage, queryWrapper);

        //4.封装响应结果
        PageResponseResult<List<WmChannel>> result = new PageResponseResult<List<WmChannel>>(dto.getPage(), dto.getSize(), ipage.getTotal(), ipage.getRecords());
        return result;
    }

    @Override
    public ResponseResult saveChannel(WmChannel entity) {
        // 参数校验
        if (entity == null || StringUtils.isEmpty(entity.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 查询当前频道名称是否已经存在,已经存在,提示不能新增
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, entity.getName());
        WmChannel one = this.getOne(query);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 保存频道
        this.save(entity);
        return ResponseResult.okResult();
    }

    /**
     * 更新频道信息
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel channel) {
        //1.参数非空校验
        if (channel == null || StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.更新频道信息
        //2.1 根据入参查询频道是否存在,存在不需要进行修改
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName, channel.getName());
        queryWrapper.eq(WmChannel::getId, channel.getId());
        WmChannel one = this.getOne(queryWrapper);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //2.2 如果频道被使用中,不能禁用
        if (!channel.getStatus()) {
            LambdaQueryWrapper<WmNews> queryNews = new LambdaQueryWrapper<>();
            queryNews.eq(WmNews::getChannelId, channel.getId());
            int count = wmNewsService.count(queryNews);
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_UPDATE);
            }
        }
        //2.3 更新频道信息
        this.updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        // 根据id查询频道
        WmChannel channel = this.getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 只有禁用的频道才能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_UPDATE);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }

}
